package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 * @author acer
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     */
    @ApiOperation(value = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


/**
 * 新增员工
 */
    @ApiOperation(value = "新增员工")
    @PostMapping()
    public Result<String> addEmployee(@RequestBody @Validated EmployeeDTO employeeDto){
    //            获得线程的id
        System.err.println("当前线程的id："+Thread.currentThread().getId());
        log.info("新增员工：{}", employeeDto);
        employeeService.addEmployee(employeeDto);
    return Result.success("添加成功了");
    }


//分页查询员工信息
    @ApiOperation(value = "分页查询员工信息")
    @GetMapping("/page")
    public Result<PageResult> getEmployeeByPage(EmployeePageQueryDTO employeePageQueryDTO){
        PageResult employeeByPage = employeeService.getEmployeeByPage(employeePageQueryDTO);
        return  Result.success(employeeByPage);
    }

//   启用禁用员工账号
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用禁用员工账号")
    public  Result statusOrStop(@PathVariable Integer status,Long id){
    log.info("状态：{},用户的id{}",status,id);
        employeeService.statusOrStop(status,id);
        return Result.success();
    }

//    查询员工信息
    @GetMapping("/{id}")
    @ApiOperation(value = "查询员工信息")
    public Result<Employee> getByEmployeeId(@PathVariable Long id){
    return Result.success(employeeService.getByEmployeeId(id));
    }


//    修改员工信息
    @ApiOperation(value = "修改员工信息")
    @PutMapping()
    public Result update(@RequestBody @Validated EmployeeDTO employeeDto){
        employeeService.update(employeeDto);
    return Result.success();
    }


}
